package com.zhangtai.modules.service.impl.leave;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.DistributeLeaveDao;
import com.zhangtai.modules.entity.leave.DistributeLeaveEntity;
import com.zhangtai.modules.service.leave.DistributeLeaveService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class DistributeLeaveServiceImpl extends ServiceImpl<DistributeLeaveDao, DistributeLeaveEntity> implements DistributeLeaveService {

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<DistributeLeaveEntity> page = this.page(
                new Page<DistributeLeaveEntity>(pageNum,pageSize),
                new QueryWrapper<DistributeLeaveEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<DistributeLeaveEntity> getLeaveList( @RequestParam(value = "oid", required = false) Long oid,
                                                           @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                           @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                                           @RequestParam(value = "uniId", required = false) List<Long> uniId,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<DistributeLeaveEntity> page = this.page(new Page<>(pageNum,pageSize),
                new QueryWrapper<DistributeLeaveEntity>().eq("is_delete","0").eq(!ObjectUtils.isEmpty(oid),"oid",oid).
                        in(!CollectionUtils.isEmpty(orgId),"org_id",orgId).in(!CollectionUtils.isEmpty(uniId),"unit_id",uniId).
                and(ObjectUtils.isEmpty(oid), war->war.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).
                        or().eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)).
                orderByDesc("leave_begin_time"));
        return new PageResult<>(page);
    }

    @Override
    public DistributeLeaveEntity getByIdE(Long id) {
            DistributeLeaveEntity one = this.getOne(new QueryWrapper<DistributeLeaveEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(DistributeLeaveEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(DistributeLeaveEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            DistributeLeaveEntity entity = new DistributeLeaveEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}